n<%
metamodel http://www.eclipse.org/uml2/2.0.0/UML
import org.acceleo.modules.uml2.services.Uml2Services
import org.acceleo.modules.uml2.services.StringServices
%>
<%script type="Class" name="taglibPackage"%>
org.taam.test.taglib
<%script type="Class" name="fullFilePath"%>
<%-- Only Classes with stereotype View--%>
<%if (hasStereotype("Entity")){%>
/<%taglibPackage.toPath()%>/<%name%>ViewTag.java
<%}%>
<%script type="Class" name="generate" file="<%fullFilePath%>"%>
package <%taglibPackage%>;


import org.acceleo.sample.dto.<%name%>Dto;
import org.acceleo.sample.jdbc.Jdbc<%name%>Dao;
import org.acceleo.fwk.dao.DaoException;


import java.sql.SQLException;
import java.util.List;
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
//<%startUserCode%> - Mettez ici vos import	
//<%endUserCode%>


public class <%name%>ViewTag extends TagSupport {


	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2618866108919171087L;


	private Iterator <<%name%>Dto>iterator;
	private List <<%name%>Dto> results;
	private HashMap<String, String> viewSet=new HashMap<String, String>();
	
	private String endMessage="dzdz";


	private int counter;
	
	@Override
	public int doStartTag() throws JspException {
		
		counter=0;
		Jdbc<%name%>Dao jdbc=new Jdbc<%name%>Dao();
		try{
		 if (!jdbc.isTableExist()){

			endMessage="erreur : table non existante!!";
		
		return SKIP_BODY;
}
else{
	List <<%name%>Dto>results=jdbc.findAll<%name%>s();
	iterator= results.iterator();
	if( iterator.hasNext()) 
					return updateContext();	
	else {
					endMessage="no match found.";		
				} 
				return SKIP_BODY;
}
} catch (DaoException e) {
		e.printStackTrace();
	throw (new JspException("Dao"));
} catch (SQLException e) {
	
	e.printStackTrace();
	throw (new JspException("SQL"));
}
	}


	@Override
public int doAfterBody() throws JspException {

	
	return	updateContext();
	
}


private int updateContext(){
if( iterator.hasNext()) 
	{counter++;
		<%name%>Dto <%name.firstLower()%> =  iterator.next();

<%for (attribute){%>
				<%if (type.filter("PrimitiveType") != null){%>
				pageContext.setAttribute("<%name.firstLower()%>",<%class.name.firstLower()%>.get<%name.firstUpper()%>());
			<%}%>				
		<%}%>
			return EVAL_BODY_AGAIN;
}
else {
	endMessage=counter+" match(s) found.";
	return SKIP_BODY;
	}
}


@Override
	public int doEndTag() throws JspException {
		
	pageContext.setAttribute("endMessage",endMessage);
		return super.doEndTag();
	}

}
